package com.zjw.dao.impl;

import com.zjw.dao.BaseDao;
import com.zjw.dao.StudentDao;
import com.zjw.entity.Student;
import com.zjw.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @projectName: studentCourseSelectionManagement
 * @package: com.zjw.dao.impl
 * @className: SudentDaoImpl
 * @author: 翟
 * @date: 2025/4/6 20:53
 */

public class StudentDaoImpl extends BaseDao implements StudentDao {
    public StudentDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public int insertStudent(Student student) throws SQLException {
        String sql = "insert into student(StudentNo , LoginPwd , StudentName , Sex , GradeId , Phone , Address , BornDate , Email , IdentityCard)"+
                "values(?,?,?,?,?,?,?,?,?,?)";
        return executeUpdate(sql , student.getStudentNo() , student.getLoginPwd() , student.getStudentName() , student.getSex() , student.getGradeId() ,
                student.getPhone() , student.getAddress() , student.getBornDate() , student.getEmail() , student.getIdentityCard());
    }

    @Override
    public int CountStudentByStudentNo(int studentNo) throws SQLException {
        String sql = "select count(1) from student where StudentNo = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , studentNo);
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
            return 0;
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
    }

    @Override
    public int selectGradeIdByStudentNo(int studentNo) throws SQLException {
        String sql = "SELECT `GradeID` FROM `student` WHERE `StudentNo` = ?";
        ResultSet resultSet = null;
        int gId = 0;
        try {
            resultSet = executeQuery(sql , studentNo);
            if (resultSet.next()){
                gId = resultSet.getInt(1);
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return gId;
    }


}
